package com.sky.service.userImpl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService{

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    @Override
    @Transactional
    public void addCartById(ShoppingCartDTO shoppingCartDTO) {
        //判空
        if(shoppingCartDTO.getDishId() == null && shoppingCartDTO.getSetmealId() == null)
        {throw new DeletionNotAllowedException(MessageConstant.ID_IS_NULL);}

         Integer Number;

        //创建购物车对象
        ShoppingCart shoppingCart;

        //获取用户id
        Long userId = BaseContext.getCurrentId();

        //判断菜品id不为null
        if (shoppingCartDTO.getDishId() != null) {

            //根据所给的id数据,查询对应数据是否存在
            shoppingCart = shoppingCartMapper.selectCartByDishId(shoppingCartDTO.getDishId(),userId,shoppingCartDTO.getDishFlavor());

            //若购物车对应数据为空
            if (shoppingCart == null) {
                shoppingCart = new ShoppingCart();

                //根据id查询菜品信息
                DishVO dishVO = dishMapper.selectById(shoppingCartDTO.getDishId());
                //设置数据
                BeanUtils.copyProperties(dishVO, shoppingCart);
                shoppingCart.setDishId(dishVO.getId());
                shoppingCart.setAmount(dishVO.getPrice());
                shoppingCart.setUserId(userId);
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());

                //判断口味是否为空
                if(shoppingCartDTO.getDishFlavor() != null ){
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                }

                shoppingCartMapper.insertShopCart(shoppingCart);

                return;
            }
            //购物车对应数据不为空
            Number = shoppingCart.getNumber()+1;

            shoppingCartMapper.updateNumber(Number,shoppingCart.getDishId());

            return;
        }

        //套餐id不为null

        //根据所给的id数据,查询对应数据是否存在
        shoppingCart = shoppingCartMapper.selectCartBySetmealId(shoppingCartDTO.getSetmealId(),userId,shoppingCartDTO.getDishFlavor());

        //若购物车对应数据为空
        if (shoppingCart == null) {

            shoppingCart = new ShoppingCart();

            //根据id查询菜品信息
            Setmeal setmeal = setmealMapper.selectSetmealById(shoppingCartDTO.getSetmealId());
            //设置数据
            BeanUtils.copyProperties(setmeal,shoppingCart);
            shoppingCart.setSetmealId(setmeal.getId());
            shoppingCart.setUserId(userId);
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.insertShopCart(shoppingCart);

            return;
        }
        //购物车对应数据不为空
        Number = shoppingCart.getNumber()+1;

        shoppingCartMapper.updatesetmealNumber(Number,shoppingCart.getSetmealId());

    }

    /**查看购物车*/
    @Override
    public List<ShoppingCart> queryShoppingCart() {
        //获取用户id
        Long userId = BaseContext.getCurrentId();

        List<ShoppingCart> shoppingCartDTO = shoppingCartMapper.selectAll(userId);
        return shoppingCartDTO;
    }

/**删除购物车中一个商品*/
    @Override
    @Transactional
    public void deleteAShop(ShoppingCartDTO shoppingCartDTO) {
        //判空
        if (shoppingCartDTO.getDishId() == null && shoppingCartDTO.getSetmealId() == null)
        { throw new DeletionNotAllowedException(MessageConstant.ID_IS_NULL);}

        //查询删除的商品Number的值,并减一
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectAll(BaseContext.getCurrentId());
        for (ShoppingCart shoppingCart : shoppingCarts) {
            if((shoppingCart.getDishId() != null && shoppingCart.getDishId() == shoppingCartDTO.getDishId())
                    || (shoppingCart.getSetmealId() != null && shoppingCart.getSetmealId() == shoppingCartDTO.getSetmealId())){
                if(shoppingCart.getNumber() > 1){
                    shoppingCartMapper.updateNumber(shoppingCart.getNumber()-1,shoppingCart.getDishId());

                    return;
                }
            }
        }
        shoppingCartMapper.deleteAShop(shoppingCartDTO);
    }

    /**清空购物车*/
    @Override
    public void cleanShoppingCart() {
        Long userid = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAll(userid);
    }
}
